<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Disclosure;
use App\Models\DisclosureType;
use Illuminate\Support\Facades\Log;

class DisclosureService
{
    public function get($search){
        $sql = Disclosure::with('project')->with('users')->with('type')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });
        return $sql;
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $res['schedule_id'] = 0;
            $res['region_id'] = 0;
            $data = Disclosure::create($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $res['schedule_id'] = 0;
            $res['region_id'] = 0;
            $data = Disclosure::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function getType($search){
        $sql = DisclosureType::when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });

        return $sql;
    }

    public function insertType($input){
        
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        
        try {
            DisclosureType::create($res);
            return true;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function updateType($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = DisclosureType::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function disclosureBindUser($input){
        $user = Disclosure::find($input['id']);
        $users = json_decode($input['users']);
        try {
            $user->users()->sync($users);
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
    
}